package com.tianhua.javs.core.func.translate.condition;

import com.tianhua.javs.client.bean.JavsSegmentBean;
import com.tianhua.javs.client.func.ITranslatorFunc;
import com.tianhua.javs.core.annotations.Segment;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * Description:
 * date: 2022/2/12
 *
 * @author shenshuai
 * @version 1.0.0
 * @since JDK 1.8
 */
@Component(value = "ifnullwithSign")
@Segment(tag = "ifnull(!",scriptFuncBean = "conditionFunc",keySuffix = ")",valueSuffix = "))")
public class IfnullwithSign implements ITranslatorFunc {
    @Override
    public JavsSegmentBean translate(String shellSegment) {
        if(shellSegment.startsWith("ifnull(!")){
            shellSegment = shellSegment.replaceFirst("ifnull\\(!","if(!Objects.isNull");
            Segment segment = IfnullwithSign.class.getAnnotation(Segment.class);
            JavsSegmentBean javsSegmentBean = JavsSegmentBean.getInstance(shellSegment,segment.keySuffix(),segment.valueSuffix());
            javsSegmentBean.setMatch(true);
            return javsSegmentBean;
        }
        return JavsSegmentBean.getInstance(shellSegment);
    }
}
